#!/bin/bash
# ***************************************************************
# *                    simula+@metz.ensam.fr                    *
# *	              GNU/linux version 2.6.0                   *
# *            software under General Public License            *
# ***************************************************************
# * copyright © 2006,2007,2009,2010,2011 COLLARD Christophe
# * copyright © 2006,2007,2009,2010,2011 Centre National de la Recherche Scientifique
# * copyright © 2006,2007,2009,2010 Arts et Métiers ParisTech
# * copyright © 2006,2007,2009,2010 Laboratoire de Physique et Mécanique des Matériaux (LPMM - CNRS)
# * copyright © 2011 Centre d'Elaboration de Matériaux et d'Etudes Structurales (CEMES - CNRS)
# ***************************************************************/

#    run MPISOL++ belongs to Message Passing Interface for Simula+ Object Libraries (MPISOL++) </br>
#    MPISOL++ is part of Simula+ <br><br>
#
#    Simula+ is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version. <br><br>
#
#    Simula+ is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details. <br><br>
#
#    You should have received a copy of the GNU General Public License
#    along with Simula+; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA


# color definition
GREEN="\\033[1;32m"
NORMAL="\\033[0;39m"
RED="\\033[1;31m"
PINK="\\033[1;35m"
BLUE="\\033[1;34m"
WHITE="\\033[0;02m"
LIGHTWHITE="\\033[1;08m"
YELLOW="\\033[1;33m"
CYAN="\\033[1;36m"


# checks Simula+ has been started correctly
if [ "$rat" != "true" ]; then
    echo -e $RED"Please, configure & execute 'run simula+ tests' to start tests"$NORMAL
    exit
fi


# look for a queue manager
queue_manager="$(which qsub)"
if [ "$queue_manager" = "" ]; then
    echo -e $BLUE"warning : no queue manager found" $NORMAL
else
    echo -e $GREEN$queue_manager "... found" $NORMAL
fi    
if [ ! -d log ]; then
    echo -e $GREEN"creating log directory" $NORMAL
    mkdir log
fi


# look for a compiler
tag=mpiicpc
compiler=$(which mpiicpc)
if [ "$compiler" = "" ]; then
   tag=mpic++
   compiler=$(which mpic++)
    if [ "$compiler" = "" ]; then
       echo -e $RED"fatal error : no compiler found" $NORMAL
       exit
   else echo -e $GREEN"using mpic++ compiler" $NORMAL
   fi
else echo -e $GREEN"using intel mpiicpc compiler" $NORMAL
fi
echo -e $GREEN$($compiler --version) $NORMAL


cd MPISOL++
if [ -f MPISOL++.exe ]; then 
    rm MPISOL++.exe
fi
$compiler -fpermissive -w -I $SimulaPlus -o MPISOL++.exe tests-MPISOL++.cpp

if [ $1 == "yes" ]; then
   if [ "$tag" = "mpiicpc" ]; then
      echo -e $GREEN"using intel mpi" $NORMAL
      mpirun -r ssh -np "$(wc -l < $PBS_NODEFILE)" ./MPISOL++.exe
   else
      echo -e $GREEN"using open mpi" $NORMAL
      mpirun -np "$(wc -l < $PBS_NODEFILE)" MPISOL++.exe
   fi
   rm MPISOL++.exe
   rm *.res
else if [ $1 == "no" ]; then
        if [ -f MPISOL++.exe ]; then 
           if [ "$tag" = "mpiicpc" ]; then
              echo -e $GREEN"using intel mpi" $NORMAL
              mpirun -r ssh -np 2 ./MPISOL++.exe
           else
              echo -e $GREEN"using open mpi" $NORMAL
              mpirun --hostfile ../mpihostfile -np 2 MPISOL++.exe
           fi
           rm MPISOL++.exe
           rm *.res
        fi
     else
        echo -e $RED"wrong argument given to script: run MPISOL++" $NORMAL
	exit
     fi
fi
cd ..
